indexApp
    .controller(
        'anaDatasetCtrl',
        function($scope, $http, $interval, $timeout) {

            /**
             * 从cookie中获取taskId
             */
            $scope.taskId = "";
            function getCookie(name) {
                var arr=document.cookie.split('; ');
                for(var i = 0 ; i < arr.length; i ++){
                    var arr2=arr[i].split('=');
                    if(arr2[0]==name){
                        return arr2[1];
                    }
                }
                return '';
            }
            $scope.taskId = getCookie("taskId");


            /**
             * 数据库信息
             */
            $scope.dbInfoList = [];
            $scope.tableList = [];
            $scope.currentDBName = "";
            $scope.currentTableName = "";
            $scope.currentTableInfo = null;
            $scope.selectedDBAndTables = [];

            /**
             * 获取所有主题库名
             */
            $scope.getAllSubjectDBs = function() {
                $http({
                    method : 'GET',
                    headers : {
                        'Content-Type' : 'application/json',
                    },
                    url : 'analysis/dataset/dbs',
                }).then(function(resp, status) {
                    console.log(resp.data);
                    $scope.status = status;
                    $scope.dbInfoList=resp.data.subjectDBs;

                }, function(resp, status) {
                    $scope.resp = resp;
                    $scope.status = status;
                });
            };


            /**
             * 查询MySQL数据库中所有表
             */
            var listMySQLTables = $scope.listMySQLTables = function() {
                $scope.mysqlDbInfoVisible = true;

                var apiMySQLTables = 'MySQLManager/mysql/v2/tables';

                $http({
                    method : 'POST',
                    headers : {
                        'Content-Type' : 'application/json',
                    },
                    url : apiMySQLTables,
                    data : {"dbName": $scope.currentDBName},
                }).then(function(resp, status) {
                    console.log(resp);
                    $scope.tableList = resp.data["data"];

                    $scope.currentTableName = $scope.tableList[0].name;
                    showTableInfo();
                }, function(resp, status) {
                    $scope.resp = resp;
                    $scope.status = status;
                });
            }



            /**
             * 显示表相关信息
             */
            var showTableInfo = $scope.showTableInfo = function () {

                /**
                 * 获取表的列头信息
                 */
                $http({
                    method : 'POST',
                    headers : {
                        'Content-Type' : 'application/json',
                    },
                    url : 'MySQLManager/mysql/v2/column-info',
                    data : {"dbName": $scope.currentDBName, "tableName": $scope.currentTableName},
                }).then(function(resp, status) {
                    console.log(resp);
                    $scope.currentTableInfo = resp.data["data"];

                    getMySQLTableSampleData();

                    getMySQLTableDesc();

                }, function(resp, status) {
                    $scope.resp = resp;
                    $scope.status = status;
                });
            };



            /**
             * 显示MySQL数据库中某表的结构
             */
            var getMySQLTableDesc = $scope.getMySQLTableDesc = function() {
                $scope.sqlUserDefinedTableDataVisible = false;
                $scope.mysqlDbTableInfoVisible = true;
            }



            /**
             * MySQL数据源配置对象
             */
            var mysqlConfig = $scope.mysqlConfig = {
                ip : "127.0.0.1",
                port : "3306",
                dbName : "",
                username : "root",
                password : "root"
            };

            /**
             * 查询MySQL数据库中某表查询样例（接口只返回5行样例数据）
             */
            var getMySQLTableSampleData = $scope.getMySQLTableSampleData = function() {

                var apiMySQLTableSampleData = 'MySQLManager/mysql/select';

                mysqlConfig["sql"] = "SELECT * FROM " + $scope.currentTableName;

                mysqlConfig.dbName = $scope.currentDBName;

                $http({
                    method : 'POST',
                    headers : {
                        'Content-Type' : 'application/json',
                    },
                    url : apiMySQLTableSampleData,
                    data : mysqlConfig,
                }).then(function(resp, status) {
                    console.log(resp);
                    $scope.mysqlTableSampleData = resp.data["data"];
                }, function(resp, status) {
                    $scope.resp = resp;
                    $scope.status = status;
                });
            }

            $scope.addATableToSelected = function () {

                function getTextOfSelectedOption(elementId) {
                    var myselect=document.getElementById(elementId);
                    var index=myselect.selectedIndex;
                    return myselect.options[index].text;
                }

                $scope.selectedDBAndTables.push({
                    "dbName": $scope.currentDBName,
                    "dbComment": getTextOfSelectedOption("dbSelect"),
                    "tableName": $scope.currentTableName,
                    "tableComment":getTextOfSelectedOption("tableSelect")
                });

            }

            $scope.delSelectedDBAndTables = function (i) {
                $scope.selectedDBAndTables.splice(i, 1);
            }

            /**
             * 查询MySQL数据库中某表查询样例（接口只返回5行样例数据）
             */
            $scope.submitConfig = function() {

                Swal({
                    title: "正在构建数据集......",
                    showConfirmButton: false,
                    showCancelButton: false,
                    showLoaderOnConfirm: true,
                });

                $http({
                    method : 'POST',
                    headers : {
                        'Content-Type' : 'application/json',
                    },
                    url : "analysis/dataset/config",
                    data : {"taskId": $scope.taskId,
                            "datasets": $scope.selectedDBAndTables},
                }).then(function(resp, status) {
                    Swal.close();
                    console.log("resp.data.message", resp.data.message);
                    if (resp.data.message !== undefined && resp.data.message !== "" ) {
                        Swal.fire(resp.data.message);
                    } else {
                        Swal.fire("完成！");

                        document.cookie="taskId="+$scope.taskId;
                        // TODO 跳转到预处理页面
                        $interval(function () {
                            location.href = "MLdataPRE.html";
                        }, 1000);
                    }


                }, function(resp, status) {
                    $scope.resp = resp;
                    $scope.status = status;
                });
            }

        });
